﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeviceSDK.NetTraffic;
using BaseHelper;

namespace DeviceSDK.NetTraffic
{
    public class TravelAnaylzeResult
    {
        public int MaxJam;
        public int TotalJamDistance;

        public SectionInfo[] JamSections;

    }
    public class TravelInfo
    {
        public int Distance;
        public int TotalSecond;
        public int TotalMin;
        public int Hour;
        public int Minute;
        public int FirstJam;
        public string FirstJamDistance;
        
        public int MaxJam;
        public int MaxJamDistance;

        public string Path;
        private float _firstDistance;

        private SectionInfo[] ltSection;

        //public int Jam
        //{
        //    get { return Jam; }
        //}

        public SectionInfo[] SectionRoad
        {
            get { return ltSection; }
        }

        public string JamStatus
        {
            get {
                if (FirstJam == 2)
                { 
                    return "轻微拥堵";
                }
                else if(FirstJam==3)
                {
                    return "严重拥堵";
                }
                else
                    return "畅通";
            }
        }

        public string JamStatusInfo
        {
            get
            {
                if (FirstJam == 2)
                {
                    if (_firstDistance < 1)
                        return "前方道路 车辆缓行";
                    else
                        return "前方" + FirstJamDistance + "公里 轻微拥堵";
                }
                else if (FirstJam == 3)
                {
                    if (_firstDistance < 1)
                        return "前方道路 车辆拥堵";
                    else
                        return "前方" + FirstJamDistance+ "公里 车辆拥堵";
                }
                else
                    return "前方道路 行驶畅通";
            }
        }

        public string JamStatusInfoOnlyJam
        {
            get
            {
                if (FirstJam >= 2)
                {
                    LogHelper.Debug("DeviceSDK.NetTraffic.TravelInfo.cs", DateTime.Now.ToString() + " JamStatusInfoOnlyJam=" + this.JamStatusInfo + ",FirstDistance=" + FirstJamDistance);
                    return this.JamStatusInfo;
                }
                return "";
            }
        }

        public TravelInfo(int distance, int duration, int firstJam, float firstDistance, int maxJam, float maxJamDistance, string path = null, SectionInfo[] sInfos = null)
        {
            Distance = (int)Math.Ceiling((decimal)distance / 1000); ;
            TotalSecond = duration % 60;
            TotalMin =(int) Math.Ceiling( (decimal)duration / 60);
            Hour = TotalMin / 60;
            Minute = TotalMin % 60;

            MaxJam = maxJam;
            MaxJamDistance = (int)maxJamDistance;
            FirstJam = firstJam;

            //如果最严重的距离距离第一次拥堵距离小于两公里，则显示最严重拥堵的。
            if (MaxJam>FirstJam &&  (maxJamDistance - firstDistance) < 1500)
            {
                FirstJam = MaxJam;
                firstDistance = maxJamDistance;
            }
            else
            {
                FirstJam = firstJam;
                firstDistance = firstDistance / 1000;
            }

            _firstDistance = firstDistance;
            if (firstDistance < 10)
            {
                FirstJamDistance = firstDistance.ToString("0.0");
            }
            else
                FirstJamDistance = Convert.ToInt32(firstDistance).ToString();

            Path = path;

           
            ltSection = sInfos;        
        }

        public TravelAnaylzeResult GetAnalyzeResult()
        {
            TravelAnaylzeResult result = new TravelAnaylzeResult();

            //如果本条路段最严重拥堵为1（畅通）
            if (MaxJam == 1)
            {
                result.MaxJam = 0;
                result.TotalJamDistance = 0;
            }
            else
            {
                result.MaxJam = MaxJam;
                List<SectionInfo> lt = new List<SectionInfo>();
                foreach (SectionInfo section in ltSection)
                {
                    if (section.Jam > 1)
                    {
                        result.TotalJamDistance += section.Distance;
                        lt.Add(section);
                    }
                }
                result.JamSections = lt.ToArray();                
            }

            return result;
        }

        
    }
}
